<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://www.yaodian100.com/jsp/yaodian100" prefix="yaodian100" %>
<%@ include file="/taglibs.jsp"%>
<!-- 数字验证 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>		
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<!-- 時間 -->	
<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>	
<s:actionerror cssClass="errmsg"/>
<s:actionmessage cssClass="errmsg"/>
  <h1>编辑资料 - 搭售商品/赠品修改</h1>
  <div class="batchstep">
    <div class="batchstep-2">
      <span class="step1">Step 1：查寻商品/选取商品</span>
      <span class="onstep2">Step 2：编辑资料</span>
      <span class="step3">Step 3：核对资料/完成编辑</span>
    </div>
  </div>
<script>
$().ready( function() {
	$('#startDateParam').datepick({minDate: new Date()-350, showOn: 'both',numberOfMonths:2,
    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	$('#endDateParam').datepick({minDate: new Date()-350, showOn: 'both', numberOfMonths:2,
    	buttonImageOnly: true, beforeShow:function(input, inst){return customRange('startDateParam');}, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	function customRange (x){
		//var startDt = $('#startDateParam').val(); //起始日
		var startDt = $('#'+x).val(); //起始日
		var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
		return {minDate:endDt}; 
	}
	$("#timestart").timePicker({step:60, startTime:"00:00", endTime:"23:00"});
	$("#timeend").timePicker({step:60, startTime:"00:59", endTime:"23:59"});

	$('input[id^=addOnPriceParam]').numeric({allow:"."});

	$("#operators").change(function(){
		if($(this).val()=='1'||$(this).val()=='2'||$(this).val()=='3'||$(this).val()=='4'){			
			$("#addOnPriceParam").val("1");
		}else if($(this).val()=='5'){
			$("#addOnPriceParam").val("0");
		}else if($(this).val()=='6'){
			$("#addOnPriceParam").val("原价格");
		}
			
	});

	$("#oldsubmit").click(function() {
		$("#dataoldornew").val("old");
		$(".sumitdisable").attr("disabled",true);     
		$("#nextsubmit").hide();
		$("#errtable").hide();	
		$("#confirmForm").submit();		
	});

	$("#mainSubmit").click(function(){
		if ( "" == $("#MultiFile8_F1").val()){
			alert("尚未选择上传档案，请重新选择！");
			return false;
		}
	});
	
	$("input[name=pid]").each(function(index){
		var key = $(this).val();

		$('#startDate_'+key).datepick({minDate: new Date(), showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$('#endDate_'+key).datepick({minDate: new Date(), showOn: 'both', numberOfMonths:2,
	    	buttonImageOnly: true, beforeShow:function(input, inst){return customRange('startDate_'+key);}, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		
		$("#startTime_"+key).timePicker({step:60, startTime:"00:00", endTime:"23:00"});
		$("#endTime_"+key).timePicker({step:60, startTime:"00:59", endTime:"23:59"});
	});
	
	$('input[id^=addOnPrice_]').numeric({allow:"."});
	$('input[id^=addOnPriceParam]').numeric({allow:"."});

	$('input[id^=addOnProductId_]').numeric({allow:"."});
	$('input[id^=addOnProductIdParam]').numeric({allow:"."});
	
	 
});
function applyAll() {
	var error = 0;
		if($("#addOnDescParam").val()=="" && $(":radio[name='tmpAddOnDesc']:checked").val()=="N"){
			$("#wordCountError").show();
			error++;
		}else{
			$("#wordCountError").hide();
		}
	if(($.trim($("#startDateParam").val())==""||$.trim($("#timestart").val())=="") && $(":radio[name='tmpstartDate']:checked").val()=="N"){		
		$("#onTimeError").show();
		error++;
	}else{
		$("#onTimeError").hide();
	}
	if(($.trim($("#endDateParam").val())==""||$.trim($("#timeend").val())=="") && $(":radio[name='tmpendDate']:checked").val()=="N"){		
		$("#offTimeError").show();
		error++;
	}else{
		$("#offTimeError").hide();
	}
	if ( !dateDiff($("#startDateParam").val() , $("#timestart").val() ,$("#endDateParam").val()  , $("#timeend").val())){
		$("#offTimeEarlyError").show();
		error++;		
	}else{
		$("#offTimeEarlyError").hide();
	}

	if($("#addOnPriceParam").val()=='请输入价格'){	
		$("#addOnPriceError").show();
		error++;
	}else{
		$("#addOnPriceError").hide();
	}

	if(parseFloat($("#addOnPriceParam").val())==0){
		if(confirm("价格为0，确定吗?")){
			$("#addOnPriceError").hide();
		}else{
			$("#addOnPriceParam").val('请输入价格');
			error++;
		}
	}
	if(error>0){
		return;
	}

	if($(":radio[name='tmpstartDate']:checked").val()=="Y"){
		$("input[name=pid]").each(function(index){
			var key = $(this).val();
			$("#startDate_"+key).val($("#oldstartDate_"+key).val());
			$("#startTime_"+key).val($("#oldstartTime_"+key).val());
		});
	}else{
		$('input[id^="startDate_"]').attr("value", $("#startDateParam").val());
		$('input[id^="startTime_"]').attr("value", $("#timestart").val());
	}
	if($(":radio[name='tmpendDate']:checked").val()=="Y"){
		$("input[name=pid]").each(function(index){
			var key = $(this).val();
			$("#endDate_"+key).val($("#oldendDate_"+key).val());
			$("#endTime_"+key).val($("#oldendTime_"+key).val());
		});
	}else{
		$('input[id^="endDate_"]').attr("value", $("#endDateParam").val());
		$('input[id^="endTime_"]').attr("value", $("#timeend").val());
	}
	
	//商品促銷小標
	if($("#shelfPromoNM").val()=="1"){
		$('input[id^="promoteName_"]').attr("value", $("#promoteNameParam").val());
	}else{
		$("input[name=pid]").each(function(index){
			var key = $(this).val();
			$("#promoteName_"+key).val($("#oldPromoName_"+key).val());
		});
	}
	//活动售价
	if($("#addOnPriceParam").val()=='请输入价格'){
		$('input[id^="addOnPrice_"]').attr("value", "0");
	}else{
		$('input[id^="addOnPrice_"]').attr("value", $("#addOnPriceParam").val());
	}
		 $("input[name=pid]").each(function(index){
			var key = $(this).val();
			var baseprice = 0;
			if($("#addOnPriceParam").val()=='原价格'){
				$("#addOnPrice_"+key).val($("#oldaddOnPrice_"+key).val());
			}else{
				var operatorprice = parseFloat($("#addOnPriceParam").val());
				var newprice =0;
				baseprice = parseFloat($("#price1_"+key).val());
				if($("#operators").val()=="1"){//加
					operatorprice=baseprice*(operatorprice/100);
					newprice = baseprice + operatorprice;
				}else if($("#operators").val()=="2"){//減
					operatorprice=baseprice*(operatorprice/100);
					newprice = baseprice - operatorprice;
				}else if($("#operators").val()=="3"){//乘
					newprice = baseprice + operatorprice;
				}else if($("#operators").val()=="4"){//除
					newprice = baseprice - operatorprice;
				}
				$("#addOnPrice_"+key).val(newprice);
			}
		});
	//简称
		//if($.trim($("#addOnDescParam").val())!="" && $(":radio[name='tmpAddOnDesc']:checked").val()=="Y"){
		if($(":radio[name='tmpAddOnDesc']:checked").val()=="Y"){
			$("input[name=pid]").each(function(index){
				var key = $(this).val();
				$("#addOnDesc_"+key).val($("#oldPromoName_"+key).val());
			});
		}else{
			$('input[id^="addOnDesc_"]').attr("value", $("#addOnDescParam").val());
		}
	//活动状态
	if($(":radio[name='tmpisEnable']:checked").val()=="Y") {
		$(':radio[id^="isEnable_Y_"]').attr("checked", true);
	} else {
		$(':radio[id^="isEnable_N_"]').attr("checked", true);
	}
	//删除
	if($(":radio[name='tmpisDelete']:checked").val()=="Y") {
		$(':radio[id^="isDelete_Y_"]').attr("checked", true);
	} else {
		$(':radio[id^="isDelete_N_"]').attr("checked", true);
	}	
	sumbitF();			
}
//日期判断,计算天数差的函数
function dateDiff(sDate1,sTime1, sDate2,sTime2){ //sDate1和sDate2是2002-12-18格式
	 var startDate = new Date(sDate1.split("-")[0],eval(sDate1.split("-")[1])-1,sDate1.split("-")[2],sTime1.split(":")[0],sTime1.split(":")[1],00); 
	 var endDate = new Date(sDate2.split("-")[0],eval(sDate2.split("-")[1])-1,sDate2.split("-")[2],sTime2.split(":")[0],sTime2.split(":")[1],00);
 if ( startDate >= endDate )return false;
 else return true;
}
function sumbitF(){
	var error = 0;
	$("input[name=pid]").each(function(index){
		var key = $(this).val();
		
		
		//简称
		if($.trim($("#addOnDesc_"+key).val())==""){		
			$("#wordCountError_"+key).show();
			error++;
		}else{
			$("#wordCountError_"+key).hide();
		}
		
		//上架起始日期时间,上架结束日期时间
		if($.trim($("#startDate_"+key).val())==""||$.trim($("#startTime_"+key).val())==""){		
			$("#onTimeError_"+key).show();
			error++;
		}else{
			$("#onTimeError_"+key).hide();
		}
		if($.trim($("#endDate_"+key).val())==""||$.trim($("#endTime_"+key).val())==""){		
			$("#offTimeError_"+key).show();
			error++;
		}else{
			$("#offTimeError_"+key).hide();
		}
		if ( !dateDiff($("#startDate_"+key).val() , $("#startTime_"+key).val() ,$("#endDate_"+key).val()  , $("#endTime_"+key).val())){
			$("#offTimeEarlyError_"+key).show();
			error++;		
		}else{
			$("#offTimeEarlyError_"+key).hide();
		}
		
		//活动售价
		if($.trim($("#addOnPrice_"+key).val())==""){		
			$("#addOnPriceError_"+key).show();
			error++;
		}else{
			$("#addOnPriceError_"+key).hide();
		}

		//if(parseFloat($.trim($("#addOnPrice_"+key).val()))==0){
		//	if(confirm("价格为0，确定吗?")){
		//		$("#addOnPriceError_"+key).hide();
		//	}else{
		//		$("#addOnPriceError_"+key).show();
		//		error++;
		//	}
		//}
		
		//活动状态
		if($("#isEnable_Y_"+key).attr("checked")==false && $("#isEnable_N_"+key).attr("checked")==false) {
			$("#isEnableError_"+key).show();
		} else {
			$("#isEnableError_"+key).hide();
		}

		//删除
		if($("#isDelete_Y_"+key).attr("checked")==false && $("#isDelete_N_"+key).attr("checked")==false) {
			$("#isDeleteError_"+key).show();
		} else {
			$("#isDeleteError_"+key).hide();
		}
		
	});

	if(error>0){
		return;
	}else{
		$("#dataoldornew").val("new");
		$("#oldsubmit").attr("disabled",true); 
		$("#nextsubmit").hide();
		$("#errtable").hide();	
		$("#confirmForm").submit();
		
	}
}
function hiddenSpan(pId) {
	
}
</script>
  <!-- div class="data-form"> -->
	<s:form action="downloadFile.do" id="confirmForm" method="post">
	<s:hidden name="paramBatchEventId"/>
      <img src="/images/ecadmin/bianjiziliaoliucheng.jpg"></img>
	<div class="tipmsg" style="width:53%"><ul><li>系统会将你所选定的商品自动生成Excel报表，请在信息编辑完成后，下载报表。<br/>此阶段具有时效性，请在<Strong>25分钟</Strong>内完成编辑，并上传相关Excel报表</li></ul></div>
     <h2>下载报表</h2>	
		<table class="tb-edit" style="width:53%;clear:none;">
          <tbody>
           <tr>
          	<th>删除</th>
          	<td>
          	<input type="radio" class="sumitdisable" value="Y" name="tmpisDelete" id="tmpisDelete_Y" >是 
          	<input type="radio" class="sumitdisable" checked="checked" value="N" name="tmpisDelete" id="tmpisDelete_N" >否
          	</td>
          </tr>
            <tr>
          	<th>*简称</th><td>
          	<input type="radio" class="sumitdisable" value="Y" name="tmpAddOnDesc">保持原值
          	<input type="radio" class="sumitdisable" checked="checked" value="N" name="tmpAddOnDesc">手动设定
           <input type="text" class="sumitdisable" name="addOnDescParam" id="addOnDescParam" size="25" maxlength="20" value="">
            <div class="errmsg" id="wordCountError" style="display:none"><ul><li><span>简称为必填</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>*活动开始时间</th><td>
          	<input type="radio" class="sumitdisable" value="Y" name="tmpstartDate">保持原值
          	<input type="radio" class="sumitdisable" checked="checked" value="N" name="tmpstartDate">手动设定
          	<input type="text" class="sumitdisable" name="startDateParam" id="startDateParam" size="20" value="<fmt:formatDate value="${shelf.startDate}" pattern="yyyy-MM-dd" />"/>	 
			<input id="timestart" class="sumitdisable" name="timestart" type="text" value="<fmt:formatDate value="${shelf.startDate}" pattern="HH:mm" />"  size="10"/>
            <div class="errmsg" id="onTimeError" style="display:none"><ul><li><span>活动开始时间为必填</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>*活动结束时间</th><td>
          	<input type="radio" class="sumitdisable" value="Y" name="tmpendDate">保持原值
          	<input type="radio" class="sumitdisable" checked="checked" value="N" name="tmpendDate">手动设定
          	<input type="text" class="sumitdisable"  name="endDateParam" id="endDateParam" size="20" value="<fmt:formatDate value="${shelf.endDate}" pattern="yyyy-MM-dd" />"/>	
			<input id="timeend" class="sumitdisable"  name="timeend" type="text" value="<fmt:formatDate value="${shelf.endDate}" pattern="HH:mm" />" size="10"/>
			<div class="errmsg" id="offTimeError" style="display:none"><ul><li><span>活动结束时间为必填</span></li></ul></div>
			<div class="errmsg" id="offTimeEarlyError" style="display:none"><ul><li><span>活动結束日期不可大于活动起始日期</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>*活动售价</th><td>
             <span id="pricetype123"">
            <select id="operators" class="sumitdisable" >
              <option value="1">原活动价加 ％</option>
              <option value="2">原活动价减 ％</option>
              <option value="3">原活动价加价</option>
              <option value="4">原活动价减价</option>  
              <option value="5">赠品</option>
              <option value="6">原活动价</option>              
            </select>            
            <input type="text" size="7" class="sumitdisable" value="请输入价格" onblur="if(this.value=='') {this.value='请输入价格';this.style.color='#aaa';this.style.background='#FFDFDF'}" onfocus="if(this.value=='请输入价格') {this.value='';}this.style.color='#000';" name="addOnPriceParam" id="addOnPriceParam" >
            </span>
            <div class="errmsg"  id="addOnPriceError" style="display:none"><ul><li><span>活动售价为必填</span></li></ul></div>
            </td>
          </tr>
          <tr>
          	<th>活动状态</th>
          	<td>
          	<input type="radio" class="sumitdisable"  checked="checked" value="Y" name="tmpisEnable" id="tmpisEnable_Y" >是 
          	<input type="radio" class="sumitdisable"  value="N" name="tmpisEnable" id="tmpisEnable_N" >否
          	</td>
          </tr>
          <tr>
            <th></th>
            <td><input type="button" class="sumitdisable" name="submit2" value="全部套用，下载Excel报表 " onclick="applyAll();">
             <input type="hidden" id="dataoldornew" name="dataoldornew">
            <c:if test="${!empty event.modifyDate}"><input type="button" id="oldsubmit" value="下载之前上传的报表 "></c:if>
            </td>
          </tr>
        </tbody></table>   	     
	   <div style="display:none">
		<s:iterator value="detailMap" var="map" status="rows_index">          
           	<input type="hidden" name="pid" value="${map.key}">
           	<input type="hidden" id="price1_${map.key}" value="${map.value.dataOldMap['addOnPrice']}">
			<c:if test="${map.value.dataOldMap['isDelete'] eq 'Y'}">
            <input type="radio" name="detailMap_${map.key}_isDelete" id="isDelete_Y_${map.key}" value="Y" checked="checked">是
           	<input type="radio" name="detailMap_${map.key}_isDelete" id="isDelete_N_${map.key}" value="N">否
            </c:if><c:if test="${map.value.dataOldMap['isDelete'] eq 'N'}">
            <input type="radio" name="detailMap_${map.key}_isDelete" id="isDelete_Y_${map.key}" value="Y" >是
           	<input type="radio" name="detailMap_${map.key}_isDelete" id="isDelete_N_${map.key}" value="N" checked="checked">否</c:if>
            <c:if test="${map.value.dataOldMap['isEnable'] eq 'Y'}">
            <input type="radio" name="detailMap_${map.key}_isEnable" id="isEnable_Y_${map.key}" value="Y" checked="checked">是
           	<input type="radio" name="detailMap_${map.key}_isEnable" id="isEnable_N_${map.key}" value="N">否</c:if>
            <c:if test="${map.value.dataOldMap['isEnable'] eq 'N'}">
            <input type="radio" name="detailMap_${map.key}_isEnable" id="isEnable_Y_${map.key}" value="Y" >是
           	<input type="radio" name="detailMap_${map.key}_isEnable" id="isEnable_N_${map.key}" value="N" checked="checked">否</c:if>
			<s:textfield size="3" name="detailMap['%{#map.key}'].dataNewMap['addOnPrice']" id="addOnPrice_%{#map.key}" value="%{#map.value.dataOldMap['addOnPrice']}"/>
           	<input type="hidden" id="oldaddOnPrice_${map.key}" value="${map.value.dataOldMap['addOnPrice']}">
           	<input type="text" name="startDate_${map.key}" id="startDate_${map.key}" size="8" value="${fn:substring(map.value.dataOldMap['startDate'],0,10)}"/>	 
		   	<input type="text" name="startTime_${map.key}" id="startTime_${map.key}" size="4" value="${fn:substring(map.value.dataOldMap['startDate'],11,16)}"/>
		   	<input type="hidden" id="oldstartDate_${map.key}" value="${fn:substring(map.value.dataOldMap['startDate'],0,10)}">
		   	<input type="hidden" id="oldstartTime_${map.key}" value="${fn:substring(map.value.dataOldMap['startDate'],11,16)}">
           	<input type="text" name="endDate_${map.key}" id="endDate_${map.key}" size="8" value="${fn:substring(map.value.dataOldMap['endDate'],0,10)}"/>	
			<input type="text" name="endTime_${map.key}" id="endTime_${map.key}" size="4" value="${fn:substring(map.value.dataOldMap['endDate'],11,16)}"/>
			<input type="hidden" id="oldendDate_${map.key}" value="${fn:substring(map.value.dataOldMap['endDate'],0,10)}">
			<input type="hidden" id="oldendTime_${map.key}" value="${fn:substring(map.value.dataOldMap['endDate'],11,16)}">
			<s:textfield size="10"  name="detailMap['%{#map.key}'].dataNewMap['addOnDesc']" id="addOnDesc_%{#map.key}" value="%{#map.value.dataOldMap['addOnDesc']}" maxlength="20"/>
           <input type="hidden" id="oldPromoName_${map.key}" value="${map.value.dataOldMap['addOnDesc']}">
           </s:iterator>
           </div>
		</s:form>
		  <br />  
		<h2>上传报表</h2>
      	 <div class="tipmsg"  style="width:53%"><ul><li>请将核对编辑后的报表上传。</li></ul></div>         
		<s:form action="uploadFile.do" method="post" enctype ="multipart/form-data">
	    	<table style="width:53%;clear:none;" border="0" align="left" class="tb-data">		
		        <tr>
		          	<td><input id="MultiFile8_F1" class="multi accept-xls" maxlength="1" type="file" name="upload"  accept="xls"/>
		          	<s:submit id="mainSubmit" value="上传Excel报表" /><br><br>
		         	<c:if test="${!empty step2Upload}">
			         	<div style="width:100%;font-size: 15px;" id="errtable" >
			         	上传成功${uploadSuccess}笔<br />
			         	上传失败${uploadErr}笔<br />
				         	<c:if test="${!empty errId}">
				         	上传失败的编号（最多显示20笔）:
					         	<c:forEach items="${errId}" var="errId" varStatus="rows_index">
					         	${errId} / 
					         	</c:forEach>	         		
				         	</c:if>
			         	</div>
		         	</c:if>
		         	</td>
		       </tr>
	    	 </table>
	   </s:form>
       <s:form action="confirm.do">
        <table width="560"  border="0"><tr height="40"><td>
     	 <br /><c:if test="${empty event.modifyDate}"><a href="changePage.do" class="pstep">﹤回上一步</a></c:if> 
      	<c:if test="${uploadSuccess>0 and uploadErr<=0}">
      		<input type="submit" id="nextsubmit" value="上传完成！下一步﹥" name="submit1" class="nstep" ><br />
		</c:if>
		</td></tr></table>
     </s:form>
    <!-- </div>  -->